package com.qf.dao.impl;

import com.qf.dao.GoodsTypeDao;
import com.qf.domain.GoodsType;
import com.qf.util.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsTypeSDaoImpl implements GoodsTypeDao {
    private QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
    @Override
    public List<GoodsType> select(int level) {
        String sql="select id,name,level,parent from tb_goods_type where level=?";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(GoodsType.class),level);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public GoodsType findById(int typeId) {
        String sql="select id,name,level,parent from tb_goods_type where id=?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(GoodsType.class),typeId);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> selectName() {
        String sql="select name from tb_goods_type where level=1";
        try {
            List<String> query = queryRunner.query(sql,new BeanListHandler<>(String.class));
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
